package com.ewanghuiju.app.ui.web;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.ewanghuiju.app.util.LogUtil;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;

import java.util.HashMap;

/**
 * Created by stone on 2018/7/4.
 */

public class QBSDKUtils {
    private static final String TAG = "QBSDKUtils";
    public static boolean isDebug = false;
    public static void init(Context context,boolean debug){
        isDebug = debug;
        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                // TODO Auto-generated method stub
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                LogUtil.e(TAG, QbSdk.isTbsCoreInited()+"onViewInitFinished: "+arg0);

            }

            @Override
            public void onCoreInitFinished() {
               // Log.e(TAG, QbSdk.isTbsCoreInited()+"onCoreInitFinished: ");
            }
        };
        QbSdk.initX5Environment(context, cb);
        //x5内核初始化接口
//        preinitX5WebCore(context,debug);


    }
    public static void preinitX5WebCore(final Context context, final boolean debug) {
        isDebug = debug;
        if (!QbSdk.isTbsCoreInited()) {
            QbSdk.preInit(context, new QbSdk.PreInitCallback() {
                @Override
                public void onCoreInitFinished() {
                    LogUtil.e(TAG, "preinitX5WebCore:onCoreInitFinished: " );
                }

                @Override
                public void onViewInitFinished(boolean b) {
                    LogUtil.e(TAG, "preinitX5WebCore:onViewInitFinished: "+ b);

                }
            });// 设置X5初始化完成的回调接口
        }
    }

//    public static void openFile(Context context,String path,OnReadListener listener){
//        onReadListener=listener;
//        Intent intent = new Intent(context,OfficeActivity.class);
//        intent.putExtra("path",path);
//        context.startActivity(intent);
//    }
    static OnReadListener onReadListener = null;
    public static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            boolean can = (boolean) msg.obj;

            if (onReadListener!=null){
                onReadListener.onResult(can);
            }
        }
    };
    public static void sendMessage(boolean can){
        Message message = new Message();
        message.obj = can;
        handler.sendMessage(message);
    }
    public interface OnReadListener{
        void onResult(boolean canReadable);
    }
}
